home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue27 / system / UPeek2 / PackPeek.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1997-10-02  |  5.3 KB  |  196 lines

  1. unit PackPeek;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ExtCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     OpenDialog: TOpenDialog;
  12.     CurrentFile: TLabel;
  13.     Button1: TButton;
  14.     Panel1: TPanel;
  15.     MainUnit: TLabel;
  16.     PackageUnit: TLabel;
  17.     WeakPackageUnit: TLabel;
  18.     ImplicitImport: TLabel;
  19.     Label1: TLabel;
  20.     UnitList: TListBox;
  21.     Panel2: TPanel;
  22.     PackageList: TListBox;
  23.     Label2: TLabel;
  24.     Panel3: TPanel;
  25.     NeverBuild: TLabel;
  26.     DesignTime: TLabel;
  27.     RunTime: TLabel;
  28.     procedure Button1Click(Sender: TObject);
  29.     procedure UnitListClick(Sender: TObject);
  30.   private
  31.     { Private declarations }
  32.   public
  33.     { Public declarations }
  34.   end;
  35.  
  36. var
  37.   Form1: TForm1;
  38.  
  39. implementation
  40.  
  41. {$R *.DFM}
  42.  
  43. function FormatPathToFit (const fName: String; Canvas: TCanvas; AvailWidth: Integer): String;
  44. var
  45.     Idx: Integer;
  46.     Drive: String[4];
  47.     Path, Name, Ext: String;
  48.  
  49.     procedure ShortenPath;
  50.     var
  51.         StartSlash: Boolean;
  52.     begin
  53.         if Path = '\' then Path := '' else begin
  54.             if Path[1] = '\' then begin
  55.                 StartSlash := True;
  56.                 Path := Copy (Path, 2, 255);
  57.             end
  58.             else StartSlash := False;
  59.  
  60.             if Path[1] = '.' then Path := Copy (Path, 5, 255);
  61.  
  62.             Idx := Pos ('\', Path);
  63.             if Idx <> 0 then Path := '...\' + Copy (Path, Idx + 1, 255)
  64.             else Path := '';
  65.  
  66.             if StartSlash then Path := '\' + Path;
  67.         end;
  68.     end;
  69.  
  70. begin
  71.     Result := fName;
  72.     Path := ExtractFilePath (Result);
  73.     Name := ExtractFileName (Result);
  74.     Idx := Pos ('.', Name);
  75.     if Idx > 0 then SetLength (Name, Idx - 1);
  76.     Ext := ExtractFileExt (Result);
  77.     if Path [2] = ':' then begin
  78.         Drive := Copy (Path, 1, 2);
  79.         Path := Copy (Path, 3, 255);
  80.     end
  81.     else Drive := '';
  82.  
  83.     while ((Path <> '') or (Drive <> '')) and (Canvas.TextWidth (Result) > AvailWidth) do
  84.     begin
  85.         if Path = '\...\' then begin
  86.             Drive := '';
  87.             Path := '...\';
  88.         end
  89.         else if Path = '' then Drive := ''
  90.         else ShortenPath;
  91.  
  92.         Result := Drive + Path + Name + Ext;
  93.     end;
  94. end;
  95.  
  96. function BoolCaption (Flags, Mask: Byte; const RootCaption: ShortString): ShortString;
  97. begin
  98.     Result := RootCaption + ': ';
  99.     if (Flags and Mask) <> 0 then Result := Result + 'Yes'
  100.     else Result := Result + 'No';
  101. end;
  102.  
  103. procedure TForm1.Button1Click (Sender: TObject);
  104. var
  105.     hLib: hModule;
  106.     rs: TResourceStream;
  107.     UnitFlags: Byte;
  108.     Idx, PackageFlags, ContainsCount, RequiresCount: Integer;
  109.  
  110.     function rsReadByte: Byte;
  111.     begin
  112.         rs.Read (Result, sizeof (Result));
  113.     end;
  114.  
  115.     function rsReadInteger: Integer;
  116.     begin
  117.         rs.Read (Result, sizeof (Result));
  118.     end;
  119.  
  120.     function rsReadString: ShortString;
  121.     var
  122.         Ch: Char;
  123.     begin
  124.         Result := '';
  125.         repeat
  126.             Ch := Char (rsReadByte);
  127.             if Ch <> #0 then Result := Result + Ch;
  128.         until Ch = #0;
  129.     end;
  130.  
  131. begin
  132.     if OpenDialog.Execute then begin
  133.         UnitList.Clear;
  134.         PackageList.Clear;
  135.         CurrentFile.Caption := FormatPathToFit (OpenDialog.FileName, Canvas, CurrentFile.Width);
  136.         hLib := LoadLibrary (PChar (OpenDialog.FileName));
  137.         if hLib <> 0 then try
  138.             { If we get here, it's a 32-bit executable }
  139.             try
  140.                 rs := TResourceStream.Create (hLib, 'PACKAGEINFO', rt_rCData);
  141.             except
  142.                 { If executable has no PackageInfo resource, just bow out }
  143.                 Exit;
  144.             end;
  145.  
  146.             { Ok, we've got the resource stream - now interpret the data }
  147.  
  148.             PackageFlags := rsReadInteger;
  149.             NeverBuild.Caption := BoolCaption (PackageFlags, 1, 'Never-Build');
  150.             DesignTime.Caption := BoolCaption (PackageFlags, 2, 'Design-Time');
  151.             RunTime.Caption := BoolCaption (PackageFlags, 4, 'Run-Time');
  152.  
  153.             RequiresCount := rsReadInteger;
  154.             if RequiresCount <> 0 then
  155.                 for Idx := 0 to RequiresCount - 1 do begin
  156.                     rsReadByte;
  157.                     PackageList.Items.Add (rsReadString);
  158.                 end;
  159.  
  160.             ContainsCount := rsReadInteger;
  161.             if ContainsCount <> 0 then begin
  162.                 for Idx := 0 to ContainsCount - 1 do begin
  163.                     UnitFlags := rsReadByte;
  164.                     rsReadByte;
  165.                     UnitList.Items.AddObject (rsReadString, TObject (UnitFlags));
  166.                 end;
  167.  
  168.                 UnitList.ItemIndex := 0;
  169.                 UnitListClick (Self);
  170.             end;
  171.  
  172.             rs.Free;
  173.         finally
  174.             FreeLibrary (hLib);
  175.         end;
  176.     end;
  177. end;
  178.  
  179. procedure TForm1.UnitListClick(Sender: TObject);
  180. var
  181.     Flags: Byte;
  182.  
  183. begin
  184.     if UnitList.ItemIndex <> -1 then begin
  185.         Flags := Byte (UnitList.Items.Objects [UnitList.ItemIndex]);
  186.  
  187.         MainUnit.Caption := BoolCaption (Flags, 1, 'Main Unit');
  188.         PackageUnit.Caption := BoolCaption (Flags, 2, 'Package unit (DPK source)');
  189.         WeakpackageUnit.Caption := BoolCaption (Flags, 4, '$WEAKPACKAGE directive');
  190.         ImplicitImport.Caption := BoolCaption (Flags, 16, 'Implicitly Imported');
  191.     end;
  192. end;
  193.  
  194. end.
  195.  
  196.